package logicaPresentacion;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import logicaNegocio.Campus;
import logicaNegocio.GestionAdministrador;


public class VerCampus extends JInternalFrame implements ActionListener,Observer
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionAdministrador gAd;
	private JTable tbCampus;			
	private String Eleccion;
    private JScrollPane scrollPane; 
    private JPanel jpBotones;
	private TablaCampus Tabla = null;
	private JButton aceptar, cancelar, borrar;
	private ClaseAObservar observable;
	private ArrayList <Campus> campus;
	private Campus C;
	
	public VerCampus(GestionAdministrador gAd,String eleccion, ClaseAObservar observable)
		{
			
			super("Campus", 
		              true, 
		              false, 
		              true, 
		              true);
			setVisible(true);
			setSize(800,300);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.inicializarSubComponentes();
			this.Eleccion = eleccion;
			this.gAd = gAd;
			this.observable = observable;
			blnCargarFormulario();
		}
		
	
	public void blnCargarFormulario()
	{
		crearTablaCampus();
		inicializarSubComponentes();
	}
	
	public void crearTablaCampus()
	{
		
		tbCampus=null;
		Tabla=new TablaCampus();
		Tabla.crearTabla(gAd.getcampus());
		tbCampus = new JTable(Tabla);
		tbCampus.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbCampus.setFillsViewportHeight(true);
		tbCampus.setEnabled(true);
		tbCampus.setRowSelectionAllowed(true);
		Tabla.fireTableDataChanged();
				
	}
	public void inicializarSubComponentes()
	{			
		try
		{
			scrollPane= new JScrollPane(tbCampus);

			this.getContentPane().setLayout(new BorderLayout());
	        this.getContentPane().add(scrollPane,"Center");
	        
	        aceptar=new JButton("Aceptar");
			aceptar.addActionListener(this);
	      
	        
			Box boxbotones;
	        if(Eleccion.equals("Borrar"))
	        {
	        	borrar=new JButton("Borrar");
	        	borrar.addActionListener(this);
				cancelar=new JButton("Cancelar");
				cancelar.addActionListener(this);
		      
		        boxbotones = Box.createHorizontalBox();
		        boxbotones.add(borrar);
		        Box.createHorizontalStrut(20);
		        boxbotones.add(cancelar);
		        
		        jpBotones=new JPanel();
				jpBotones.setLayout(new FlowLayout());
				jpBotones.add(boxbotones);
	        }
	        else
	        {
	        
		        aceptar=new JButton("Aceptar");
				aceptar.addActionListener(this);
				jpBotones=new JPanel();
				jpBotones.setLayout(new FlowLayout());
				jpBotones.add(aceptar);
	        }
			
			
			this.getContentPane().add(jpBotones,"South");
		}
		catch(Exception e)
		{
		}			
	}
		
	public void actionPerformed(ActionEvent e)
	{	
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if ((btnPulsado==aceptar)||(btnPulsado==cancelar)) 
			{
				dispose();
			}
			if(btnPulsado==borrar)
			{
				int row = tbCampus.getSelectedRow();
				if(row>=0)
				{
					campus = gAd.getcampus();
					
					C = campus.get(row);//4 es el dni

					gAd.borrarCampus(C);

					observable.setTabla();
					
					JOptionPane.showMessageDialog(null, "Tupla Borrada");
					dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Debe seleccionar una fila si quiere borrar una tupla");          
				}
			}						
		}
	}
	
	public void update(Observable o, Object arg) 
	{
		tbCampus=null;
		Tabla.crearTabla(gAd.getcampus());
		tbCampus = new JTable(Tabla);
		tbCampus.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbCampus.setFillsViewportHeight(true);
		tbCampus.setEnabled(true);
		tbCampus.setRowSelectionAllowed(true);
		Tabla.fireTableDataChanged();
		
	}
 
	class TablaCampus extends AbstractTableModel
    {
 		/**
		 * 
		 */
		private static final long serialVersionUID = -4788354365788065479L;
		private String[] columnNames = {"ID_Campus","Nombre","N�Alumnos", "Ciudad", "telefono"};
		private Object[][] data;
	        
        public void crearTabla(ArrayList<Campus> campus)
        {
        	
    		int filas = campus.size();
    		data=new Object[filas][];

    		if (Eleccion == "Por Ciudad")
    		{
    			campus = gAd.ordenarCampus(campus);
    		}
    		if (Eleccion == "Por Nombre del Campus")
    		{
    			campus = gAd.ordenarCampusNombre(campus);
    		}
    		for (int cont = 0;  cont<campus.size();cont++)
    		{
    			Campus c = (Campus) campus.get(cont);
    			Object[]a={
    					   new String(Integer.toString(c.getID())),
    					   new String(c.getNombreCampus()),
    					   new String(Integer.toString(c.getNoAlumnos())),
    					   new String(c.getCiudadCampus()),    					   
    					   new Integer(c.getTelefonoCampus()),
			};
			data[cont]=a;
	    		
    		}	        	
        }
        
        public int getColumnCount() 
	    {
	        return columnNames.length;
	    }
	
	    public int getRowCount() {
	        return data.length;
	    }
	
	    public String getColumnName(int col) 
	    {
	        return columnNames[col];
	    }
	
	    public Object getValueAt(int row, int col) 
	    {
	        return data[row][col];
	    }
    }
}
